<?php

/**
 * Description of cUrl
 *
 * @author sifat
 */
class cUrl {

    private $_c;
    public $url;
    public $agent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.2 Safari/537.36";
    public $postFields = '';
    public $cookieFileName = "cookie.text";
    public $cookieFileLocation = '';
    public $useAuth = false;
    public $outPut;

    function __construct() {
        $this->cookieFileLocation = dirname(__FILE__) . '/../' . $this->cookieFileName;
        $this->_c = curl_init();
    }

    function __destruct() {
        curl_close($this->_c);
    }

    function exe() {
        $this->_setOptions();
        $this->outPut = curl_exec($this->_c);

        return $this->outPut;
    }

    private function _setOptions() {
        curl_setopt($this->_c, CURLOPT_URL, $this->url);
        curl_setopt($this->_c, CURLOPT_USERAGENT, $this->agent);

        if ($this->postFields) {
            curl_setopt($this->_c, CURLOPT_POST, 1);
            curl_setopt($this->_c, CURLOPT_POSTFIELDS, $this->postFields);
        }
        if ($this->useAuth) {
            curl_setopt($this->_c, CURLOPT_COOKIEJAR, $this->cookieFileLocation);
            curl_setopt($this->_c, CURLOPT_COOKIEFILE, $this->cookieFileLocation);
        }

        curl_setopt($this->_c, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($this->_c, CURLOPT_FOLLOWLOCATION, 1);
    }

    function setCookieFileLocation($path) {
        $this->cookieFileLocation = $path;
    }

    public function __toString() {
        return $this->outPut;
    }

}

